package com.example.second;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/*
 * 数据库管理类
 * */
public class DBOpenHelper extends SQLiteOpenHelper {

    public DBOpenHelper(@Nullable Context context) {
        super(context, "book.db", null, 7);
    }
    // 第一次加载数据库时，会调用的函数
    /**
     * 第一次调用MyOpenHelper的时候会被执行，只执行一次（myBook.db已存在）
     *
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库  表名 book  ：  id 主键自增   bookname  ， author
        db.execSQL("create table book(_id integer primary key autoincrement,bookname varchar(10) not null,author varchar(10))");
        db.execSQL("insert into book(bookname,author) values('三国演义','罗贯中')");
        db.execSQL("insert into book(bookname,author) values(?,?)", new String[]{"西游记", "吴承恩"});
        db.execSQL("insert into book(bookname,author) values(?,?)", new String[]{"水浒传", "施耐庵"});
        db.execSQL("insert into book(bookname,author) values(?,?)", new String[]{"红楼梦", "曹雪芹"});
    }

    /**
     * 数据库版本升级时调用
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //user表     id  username  age  phone
        db.execSQL("create table user(_id integer primary key autoincrement,username varchar(10) not null,phone varchar(10),age integer)");
        db.execSQL("insert into user(username,phone,age) values(?,?,?)", new String[]{"TOM", "13311112222", "18"});
        db.execSQL("insert into user(username,phone,age) values(?,?,?)", new String[]{"JACK", "15566665555", "19"});
        db.execSQL("insert into user(username,phone,age) values(?,?,?)", new String[]{"LUCY", "18877774444", "17"});
    }}